<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class ArticleController extends BaseController {
	/*
	 * 添加文档
	 */
	public function add(){
		$this->methodcontrol(null,'add','添加成功','添加失败',null);
	}
	/*
	 * 编辑文档
	 */
	public function edit($id=null){
		$id = htmlspecialchars($id);
		if(!empty($id)){
			$this->methodcontrol($id,'save','编辑成功','编辑失败',U('Index/articlelist'));
		}else{
			$this->error('非法参数！','',1);
		}
	}

	/*
	 * 添加、编辑公用方法
	 */
	protected function methodcontrol($id=null,$method,$success_message,$error_message,$bakurl){

		$rules = array(
			array('title','require','标题不能为空！'),
		);
		$article = D(C('TABLE_ARTICLE'));
		if(!$article->validate($rules)->create()){
			$this->error($article->getError(),'',1);
		}else{
			$extendattrs = "";
			foreach(I('post.extendattr') as $v){
				$extendattrs .= $v.",";
			}
			$content 			 = html_entity_decode(I('post.content'));
			if($method == 'add') $article->pv    = mt_rand(120,498);
			$article->videopath  = html_entity_decode(I('post.videopath'));
            $article->audiopath  = html_entity_decode(I('post.audiopath'));
			$article->content    = $content;
			$article->description= cutArticle($content, 50);
			$article->extendattr = rtrim($extendattrs,',');
			$article->editdate 	 = date("Y-m-d H:i:s", time());
			$article->state	     = session('userinfo')['audit_r'];
			if($method == 'save') $article->pubdate = I('post.pubdate');
			$where['id'] = !empty($id) ? $id : null;
			$result = $article->where($where)->$method();
			if($result !== false){
				//A(C('TABLE_LOG'))->index(1,'添加文档成功');
				$this->success($success_message,$bakurl);
			}else{
				//A(C('TABLE_LOG'))->index(0,'添加文档失败');
				$this->error($error_message,'',1);
			}
		}
	}


}